var api = require("../../../../api.js"),
    utils = require("../../../../utils/utils.js"),
    app = getApp();
var img_str = '';

Page({

    /**
     * 页面的初始数据
     */
    data: {

    },

    // 跳转基因检测记录 
    togene: function () {
        wx.navigateTo({
            url: '/page/health/pages/record/record?card=' + this.data.card,
            success: function (res) { },
            fail: function (res) { },
            complete: function (res) { },
        })
    },

    // 表单提交
    formSubmit: function (e) {
        console.log(e.detail.value)
        console.log(JSON.stringify(this.data.form_1))
        app.request({
            url: api.form.submit,
            data: {
                form_list: JSON.stringify(this.data.form_1),
                diy_id: 1,
                card: this.data.card,
                // id:105
            },
            header: {
                'content-type': "application/x-www-form-urlencoded"
            },
            method: 'POST',
            dataType: 'json',
            responseType: 'text',
            success: function (res) {
                console.log(res)
                wx.showToast({
                    title: res.msg,
                    icon: 'success',
                    duration: 1500,
                    mask: true,
                })
                if (res.code == 0) {
                    wx.navigateTo({
                        url: '/page/health/pages/archives/archives?card=' + this.data.card,
                    })
                }
            },
            fail: function (res) { },
            complete: function (res) { },
        })
    },
    // checkbox change
    check_change: function (t) {
        console.log(t.target.dataset.id);
        var e = t.target.dataset.pid,
            a = t.target.dataset.id,
            o = this.data.form_1,
            i = o[e].default[a].selected;
        o[e].default[a].selected = 1 != i, this.setData({
            form_1: o
        });
    },
    // uploadimg
    checkimg: function (t) {
        console.log('点击选择图片');
        self = this
        wx.chooseImage({
            count: 9, // 默认9
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function (res) {
                // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
                var tempFilePaths = res.tempFilePaths
                self.setData({
                    imglist: tempFilePaths
                })
                //图片
                var imglist = self.data.imglist
                var img_l = new Array();

                if (imglist != '') {

                    //开始插入图片
                    for (var i = 0; i < imglist.length; i++) {
                        //上传至服务器
                        wx.uploadFile({
                            url: 'https://lhj.sjwke.com/web/index.php?r=api%2Fdefault%2Fupload-image',

                            filePath: imglist[0],
                            name: 'image',
                            success: function (res) {
                                if (i >= imglist.length) {
                                    self.setData({
                                        imglist: []
                                    })
                                    // wx.hideLoading();
                                    wx.showToast({
                                        title: '上传成功',
                                        icon: 'success',
                                        duration: 2000,
                                        mask: true
                                    })
                                }

                                img_l.push(JSON.parse(res.data).data.url)
                                img_str += JSON.parse(res.data).data.url + ','
                                // console.log()
                                // 上传到服务器的图片
                                console.log(img_l)

                                var img_arr = img_l.join(',');
                                var o = self.data.form_1;
                                var e = t.target.dataset.id;
                                o[e].default = img_arr;
                                self.setData({
                                    form_1: o
                                })
                                console.log("=====================================")
                                self.setData({
                                    url_img: JSON.parse(res.data).data.url,
                                    // img_l:img
                                })
                            }
                        })
                    }
                    console.log(img_str)
                    console.log("---------------6666---------------")
                    console.log(typeof (img_l));
                } else {
                    wx.hideLoading();
                }
            }
        })
    },
    // radio change
    radio: function (t) {
        console.log(t)
        var that = this;
        var e = t.target.dataset.pid;
        var radio_id = t.target.dataset.id;
        var a = that.data.form_1;
        for (var o in a[e].default)
            t.target.dataset.id == o ? a[e].default[o].selected = !0 : a[e].default[o].selected = !1;
        console.log(t.target.dataset.id == o ? a[e].default[o].selected = !0 : a[e].default[o].selected = !1);
        console.log("11111")
        this.setData({
            form_1: a,
            radio_id: radio_id
        });
    },
    // input value change
    input_change: function (t) {
        var that = this;
        console.log(t)
        var e = t.target.dataset.id;
        var o = this.data.form_1;
        o[e].default = t.detail.value;
        console.log(o[e].default)
        console.log(t.detail.value)
        that.setData({
            form_1: o
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this
        var user_id = options.user_id;
        that.setData({
            user_id: user_id
        })
        app.request({
            url: api.form.div_form,
            data: {
                id: 1,
                create_user: user_id
            },
            header: {},
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            success: function (res) {
                console.log("form_list=================")
                that.setData({
                    form_1: res.data.form1,
                    form_2: res.data.form2,
                    form_3: res.data.form3,
                    formData: res.data.formData
                })
            },
            fail: function (res) { },
            complete: function (res) {

            },
        })



        wx.getStorage({
            key: 'card',
            success: function(res) {
                console.log(res._card)
                console.log('=================card')
                that.setData({
                    card:res.data._card
                })
                app.request({
                    url: api.form.div_form,
                    data: {
                        card: res.data._card
                    },
                    header: {
                        'content-type': "application/x-www-form-urlencoded"
                    },
                    method: 'GET',
                    dataType: 'json',
                    responseType: 'text',
                    success: function (res) {
                        console.log(res.data)
                        that.setData({
                            detail: res.data
                        })
                    },
                    fail: function (res) { },
                    complete: function (res) { },
                })
            },
        })

       
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        var that = this
        //    调取自定义表单
      
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var that = this;
        wx.getStorage({
            key: 'card',
            success: function (res) {
                console.log(res)
                that.setData({
                    card: res.data._card
                })
            },
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})

function objToArray(array) {
    var arr = []
    for (var i in array) {
        arr.push(array[i]);
    }
    console.log(arr);
    return arr;
}